package net.bithappens.hnqe.validator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.bithappens.hnqe.InvalidDataException;
import net.bithappens.hnqe.graph.EntryProperty;
import net.bithappens.hnqe.graph.GraphNode;
import net.bithappens.hnqe.graph.GraphRelation;
import net.bithappens.hnqe.graph.GraphRelationMember;
import net.bithappesn.hnqe.traverse.GraphPath;
import net.bithappesn.hnqe.traverse.IPathValidator;

public class SimpleTypeValidator implements IPathValidator{

	private List<String> validTypes = new ArrayList<String>();
	
	@Override
	public String getValidatorId() {
		return "SimpleTypeValidator";
	}

	@Override
	public void setParams(Object... params) throws InvalidDataException {
		
		for(Object param : params){
			if(param instanceof String){
				validTypes.add((String) param);
			} else {
				throw new InvalidDataException("The provided parameters must contain only strings " +
						"declaring valid types for node and relations");
			}
		}
		
	}

	@Override
	public Collection<GraphRelationMember> getValidRelationMemberFromSet(Collection<GraphRelationMember> relationMembers, GraphPath currentPath) {

		Set<GraphRelationMember> validRelationMembers = new HashSet<GraphRelationMember>();
		
		for(GraphRelationMember member : relationMembers){
			if(member instanceof GraphNode){
				if(validTypes.contains(member.getTypeId())){
					validRelationMembers.add(member);
				}
			}
		}
		
		return validRelationMembers;
	}

	@Override
	public Collection<GraphRelation> getValidRelationsFromSet(Collection<GraphRelation> relations, GraphPath currentPath) {
		
		Set<GraphRelation> validRelations = new HashSet<GraphRelation>();
		
		for(GraphRelation relation : relations){
			if(validTypes.contains(relation.getTypeId())){
				validRelations.add(relation);
			}
		}
		
		return validRelations;
	}

}
